﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using Contract.Domain;
using DevExpress.XtraLayout.Utils;
using Framework.Domain;
using DevExpress.XtraEditors;
using System.IO;
using System.Drawing.Imaging;
using System.Collections;
using Framework;
using DevExpress.XtraGrid.Views.Grid;
using System.Reflection;

namespace CIIS
{
    public partial class SecretPerson : BaseForm
    {
        t_tq_tqry tqry
        {
            get { return BindEntity as t_tq_tqry; }
        }

        public SecretPerson()
        {
            InitializeComponent();

            layoutControlItemPrint.Visibility = LayoutVisibility.Never;

            buttonSave.DialogResult = DialogResult.OK;
            buttonSave.Click += new EventHandler(buttonSave_Click);
            buttonPrint.Click += new EventHandler(buttonPrint_Click);
            buttonBack.Click += new EventHandler(buttonBack_Click);
            //buttonNextStep.Click += new EventHandler(buttonNextStep_Click);

            pictureBoxZp.Click += new EventHandler(panelControlPhoto_Click);

            textEditSfzh.Validated += new EventHandler(textEditSfzh_Validated);

            DevControlUtils.Instance.InitLookUpEdit(lookUpEditGj, dao.GetDropdownControlParam<bm_tq_gj>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditGnlb, dao.GetDropdownControlParam<bm_tq_gnlb>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditHdzcfl, dao.GetDropdownControlParam<bm_tq_hdzcfl>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditLyfl, dao.GetDropdownControlParam<bm_tq_lyfl>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditMz, dao.GetDropdownControlParam<bm_tq_mz>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditQklx, dao.GetDropdownControlParam<bm_tq_qklx>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditWhcd, dao.GetDropdownControlParam<bm_tq_whcd>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditXb, dao.GetDropdownControlParam<bm_tq_xb>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditZy, dao.GetDropdownControlParam<bm_tq_zy>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditZydxfl, dao.GetDropdownControlParam<bm_tq_zydxfl>());
            DevControlUtils.Instance.InitLookUpEdit(lookUpEditZkdmc,dao.ZczdDcp);

            addressJg.InitControl(new bool[] { true, true, false }, new string[] { "籍贯：", "", "" });
            addressHkszd.InitControl(new bool[] { true, true, true }, new string[] { "户籍地：      ", "", "" });
            addressXzz.InitControl(new bool[] { true, true, true }, new string[] { "现住地：      ", "", "" });

            normalSignatureCbdwyj.InitControl("特管单位意见：", "特管单位签字：", "签字时间：",dao.UserDcp);
            normalSignatureFjyj.InitControl("分局意见：", "签字：", "签字时间：", dao.UserDcp);
            normalSignatureJhglyj.InitControl("今后管理意见：", "签字：", "签字时间：", dao.UserDcp);
            normalSignatureLdyj.InitControl("领导意见：", "签字：", "签字时间：", dao.UserDcp);
            normalSignatureTgbmsh.InitControl("特管部门审核", "签字：", "签字时间：", dao.UserDcp);

            DevControlUtils.Instance.InitRepositoryItemLookUpEdit(repositoryItemLookUpEdit1, dao.GetDropdownControlParam<bm_tq_xb>());
        }

        void textEditSfzh_Validated(object sender, EventArgs e)
        {
            string id = textEditSfzh.Text;
            if (!IdCardUtils.Instance.CheckIDCard(id))
            {
                XtraMessageBox.Show("身份证号不符合要求请重新输入");
                return;
            }
            dateEditCsny.DateTime = IdCardUtils.Instance.GetBirthdayFromIdCard(id);
            lookUpEditXb.EditValue = IdCardUtils.Instance.GetSexFlagFromIdCard(id).ToString();
        }

        void panelControlPhoto_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "图片文件(*.jpg,*.gif,*.bmp)|*.jpg|*.gif|*.bmp";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Image pic = Image.FromFile(dlg.FileName);
                    pictureBoxZp.BackgroundImage = pic;
                    pictureBoxZp.BackgroundImageLayout = ImageLayout.Stretch;
                }
            }
            catch
            {
                XtraMessageBox.Show("打开图片文件失败。");
            }
        }

        public byte[] GetImageBuffer(Image imgPhoto)
        {
            //将Image转换成流数据，并保存为byte[] 
            MemoryStream mstream = new MemoryStream();
            imgPhoto.Save(mstream, ImageFormat.Bmp);
            byte[] byData = new Byte[mstream.Length];
            mstream.Position = 0;
            mstream.Read(byData, 0, byData.Length);
            mstream.Close();
            return byData;
        }

        private void InitControls()
        {
            if (OptType == EOperateType.Detail)
            {
                layoutControlItemSave.Visibility = LayoutVisibility.Never;
            }
        }

        void buttonNextStep_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }

        void buttonPrint_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (OptType == EOperateType.Add)
                {
                    BindEntity.State.MarkNew();
                }
                else
                {
                    BindEntity.State.MarkDirty();
                }
                tqry.CSNY = dateEditCsny.DateTime.ToString("yyyy-MM-dd");
                if (pictureBoxZp.BackgroundImage != null)
                {
                    //tqry.XP = GetImageBuffer(pictureBoxPhoto.BackgroundImage);
                }

                tqry.CBDW_YJ = normalSignatureCbdwyj.Yj;
                tqry.CBDW_QZ = normalSignatureCbdwyj.Qz;
                tqry.CBDW_CBSJ = normalSignatureCbdwyj.Qzsj;
                tqry.FJZD_YJ = normalSignatureFjyj.Yj;
                tqry.FJZD_QZ = normalSignatureFjyj.Qz;
                tqry.FJZD_SHSJ = normalSignatureFjyj.Qzsj;
                tqry.JHGLYJ = normalSignatureJhglyj.Yj;
                tqry.SYMJ = normalSignatureJhglyj.Qz;
                tqry.SYSJ = normalSignatureJhglyj.Qzsj;
                tqry.SJLD_YJ = normalSignatureLdyj.Yj;
                tqry.SJLD_QZ = normalSignatureLdyj.Qz;
                tqry.SJLD_SPSJ = normalSignatureLdyj.Qzsj;
                tqry.TGBM_YJ = normalSignatureTgbmsh.Yj;
                tqry.TGBM_QZ = normalSignatureTgbmsh.Qz;
                tqry.TGBM_SHSJ = normalSignatureTgbmsh.Qzsj;

                tqry.HKSZD = addressHkszd.GetAddress();
                tqry.JG = addressJg.GetAddress();
                tqry.XZZ = addressXzz.GetAddress();

                //todo pxc 默认分局未审核，需要调整
                tqry.SHZT = "00";

                gridControlJtshgx.SaveData();

                t_tq_tqry o = dao.Dao.SaveOrUpdate(BindEntity) as t_tq_tqry;
                if (pictureBoxZp.BackgroundImage != null)
                {
                    Utils.Instance.SaveImage(pictureBoxZp.BackgroundImage, tqry.RYDH);
                }
                XtraMessageBox.Show("保存成功");
            }
            catch
            {
                XtraMessageBox.Show("保存失败");
            }
            finally
            {
                Close();
            }
        }

        /// <summary>
        /// 绑定数据源，在show窗口之前需要调用
        /// </summary>
        /// <param name="tqry"></param>
        public override void BindData(Entity entity)
        {
            InitControls();
            BindEntity = entity as t_tq_tqry;
            if (BindEntity == null)
            {
                BindEntity = new t_tq_tqry();
                tqry.RYDH = Utils.Instance.GetCode("t_tq_tqry", "RYDH");
            }

            if (tqry != null)
            {
                //textEditAddr.DataBindings.Add("Text", tqry, "HKSZD");
                //lookUpEditJgS.DataBindings.Add("Text", tqry, "JG");
                //textEditComAddr.DataBindings.Add("Text", tqry, "LLDZ");
                //textEditCurrentAddr.DataBindings.Add("Text", tqry, "XZZ");
                //textEditZkdmc.DataBindings.Add("Text", tqry, "ZKDMC");
                //textEditTel.DataBindings.Add("Text", tqry, "LXDH");
                textEditGzdw.DataBindings.Add("Text", tqry, "GZDW");
                textEditHjdxz.DataBindings.Add("Text", tqry, "HKSZDXZ");
                textEditHm.DataBindings.Add("Text", tqry, "BM");
                textEditLxdh.DataBindings.Add("Text", tqry, "LXDH");
                textEditRydh.DataBindings.Add("Text", tqry, "RYDH");
                textEditSfzh.DataBindings.Add("Text", tqry, "SFZH");
                textEditXm.DataBindings.Add("Text", tqry, "XM");
                textEditXzdxz.DataBindings.Add("Text", tqry, "XZZXZ");

                lookUpEditGj.DataBindings.Add("EditValue", tqry, "GJ");
                lookUpEditGnlb.DataBindings.Add("EditValue", tqry, "GNLB");
                lookUpEditHdzcfl.DataBindings.Add("EditValue", tqry, "ZCCDFL");
                lookUpEditLyfl.DataBindings.Add("EditValue", tqry, "LYFL");
                lookUpEditMz.DataBindings.Add("EditValue", tqry, "MZ");
                lookUpEditQklx.DataBindings.Add("EditValue", tqry, "QKLX");
                lookUpEditWhcd.DataBindings.Add("EditValue", tqry, "WHCD");
                lookUpEditXb.DataBindings.Add("EditValue", tqry, "XB");
                lookUpEditZkdmc.DataBindings.Add("EditValue", tqry, "ZKDMC");
                lookUpEditZy.DataBindings.Add("EditValue", tqry, "GZDW");
                lookUpEditZydxfl.DataBindings.Add("EditValue", tqry, "ZYDXFL");

                memoEditBxqk.DataBindings.Add("Text", tqry, "BXQK");
                memoEditQkqk.DataBindings.Add("Text", tqry, "QKQK");
                memoEditBz.DataBindings.Add("Text", tqry, "BZ");
                memoEditGrjl.DataBindings.Add("Text", tqry, "GRJL");
                memoEditZygzrw.DataBindings.Add("Text", tqry, "ZYGZRW");

                normalSignatureCbdwyj.SetValues(new object[] { tqry.CBDW_YJ, tqry.CBDW_QZ, tqry.CBDW_CBSJ });
                normalSignatureFjyj.SetValues(new object[] { tqry.FJZD_YJ, tqry.FJZD_QZ, tqry.FJZD_SHSJ });
                normalSignatureJhglyj.SetValues(new object[] { tqry.JHGLYJ, tqry.SYMJ, tqry.SYSJ });
                normalSignatureLdyj.SetValues(new object[] { tqry.SJLD_YJ, tqry.SJLD_QZ, tqry.SJLD_SPSJ });
                normalSignatureTgbmsh.SetValues(new object[] { tqry.TGBM_YJ, tqry.TGBM_QZ, tqry.TGBM_SHSJ });
                //normalSignatureCbdwyj.BindData(tqry, new string[] { "CBDW_YJ", "CBDW_QZ", "CBDW_CBSJ" });
                //normalSignatureFjyj.BindData(tqry, new string[] { "FJZD_YJ", "FJZD_QZ", "FJZD_SHSJ" });
                //normalSignatureJhglyj.BindData(tqry, new string[] { "JHGLYJ", "SYMJ", "SYSJ" });
                //normalSignatureLdyj.BindData(tqry, new string[] { "SJLD_YJ", "SJLD_QZ", "SJLD_SPSJ" });
                //normalSignatureTgbmsh.BindData(tqry, new string[] { "TGBM_YJ", "TGBM_QZ", "TGBM_SHSJ" });

                gridControlJtshgx.KeyString = "RYDH";
                gridControlJtshgx.KeyValue = tqry.RYDH;
                gridControlJtshgx.DataType = typeof(t_tq_jtcy);
                QueryInfo info = new QueryInfo();
                info.QueryObject = "t_tq_jtcy";
                info.AddParam("RYDH", string.IsNullOrWhiteSpace(tqry.RYDH) ? "-1" : tqry.RYDH);
                IList gx = dao.Dao.FindList(info);
                if (gx.Count == 0)
                {
                    gx.Add(new t_tq_jtcy() { RYDH = tqry.RYDH, CYGX = "", CYGZDW = "", CYNL = 0, CYXB = "", CYXM = "" });
                }
                //DataTable table = Utils.Instance.ToDataTable(gx);
                gridControlJtshgx.DataSource = gx;
                gridControlJtshgx.RefreshDataSource();

                if (!string.IsNullOrWhiteSpace(tqry.HKSZD))
                {
                    string[] hjd = tqry.HKSZD.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (hjd != null && hjd.Length == 3)
                    {
                        addressHkszd.SetEditValue(hjd);
                    }
                }
                if (!string.IsNullOrWhiteSpace(tqry.JG))
                {
                    string[] jg = tqry.JG.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (jg != null && jg.Length == 2)
                    {
                        addressJg.SetEditValue(jg);
                    }
                }
                if (!string.IsNullOrWhiteSpace(tqry.XZZ))
                {
                    string[] xzz = tqry.XZZ.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (xzz != null && xzz.Length == 3)
                    {
                        addressXzz.SetEditValue(xzz);
                    }
                }

                DateTime dt;
                if (DateTime.TryParse(tqry.CSNY, out dt))
                {
                    dateEditCsny.DateTime = dt;
                }
                if (tqry != null)
                {
                  info = new QueryInfo();
                    info.QueryObject = "t_ry_xp";
                    info.AddParam("RYDH", tqry.RYDH);
                    t_ry_xp c = dao.Dao.FindOne(info) as t_ry_xp;
                    if (c != null)
                    {
                        MemoryStream ms = new MemoryStream(c.XP);
                        Image image = Image.FromStream(ms);
                        pictureBoxZp.BackgroundImage = image;
                        pictureBoxZp.BackgroundImageLayout = ImageLayout.Stretch;
                    }
                }

                //if (tqry.XP != null)
                //{
                //    MemoryStream ms = new MemoryStream(tqry.XP);
                //    Image image = Image.FromStream(ms);
                //    pictureBoxPhoto.BackgroundImage = image;
                //    pictureBoxPhoto.BackgroundImageLayout = ImageLayout.Stretch;
                //}
            }
        }

    }
}
